package com.test.miniotest.minio.controller;

import com.alibaba.fastjson.JSONObject;
import com.test.miniotest.minio.utils.MinIOUtil;
import io.minio.errors.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@RequestMapping("/minio")
@RestController
public class MinioController {

    @Autowired
    private MinIOUtil minIOUtil;


    /**
     * 创建存储桶
     *
     * @param bucketName
     * @return
     */
    @PostMapping("/createBucket")
    public boolean createBucket(@RequestParam("bucketName") String bucketName) {
        try {
            if (StringUtils.isBlank(bucketName)) {
                return false;
            }
            if (!minIOUtil.bucketExists(bucketName)) {
                minIOUtil.createBucket(bucketName);
                return true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 获取存储桶策略
     *
     * @param bucketName 存储桶
     * @return json
     */
    @PostMapping("/getBucketPolicy")
    public JSONObject getBucketPolicy(String bucketName) {
        try {
            return minIOUtil.getBucketPolicy(bucketName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 上传
     *
     * @param multpartFile
     * @param bucketName   存储桶
     * @param filePath     文件存放路径（必须以/开始和以/结尾），不包含文件名。例如：/file/pdf/2022/2/23/
     * @return 文件存储在服务器上的完整路径
     */
    @RequestMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile multpartFile, @RequestParam("bucketName") String bucketName, @RequestParam("filePath") String filePath) {
        try {
            String upload = minIOUtil.upload(bucketName, filePath, multpartFile);
            return upload;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 下载文件
     *
     * @param bucketName 存储桶
     * @param objectName 文件存储在服务器上的完整路径
     */
    @RequestMapping("/download")
    public void download(HttpServletResponse response, @RequestParam("bucketName") String bucketName, @RequestParam("objectName") String objectName) {
        try {
            minIOUtil.download(response, bucketName, objectName);
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        }
    }


    /**
     * 拷贝文件
     *
     * @param bucketName    源文件存储桶
     * @param objectName    源文件存储路径（以/开头，包含文件名），如：/2022/02/01/1645404169.pdf
     * @param srcBucketName 目标存储桶
     * @param srcObjectName 目标文件存放路径（必须以/开始和以/结尾），不包含文件名。例如：/file/pdf/2022/2/23/
     */
    @RequestMapping("/copyFile")
    public String copyFile(@RequestParam("bucketName") String bucketName, @RequestParam("objectName") String objectName, @RequestParam("srcBucketName") String srcBucketName, @RequestParam("srcObjectName") String srcObjectName) {
        try {
            String s = minIOUtil.copyFile(bucketName, objectName, srcBucketName, srcObjectName);
            return s;
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    /**
     * 删除文件
     *
     * @param bucketName 存储桶
     * @param objectName 文件存储路（以/开头，包含文件名），如：/2022/02/01/1645404169.pdf
     */
    @RequestMapping("/delete")
    public boolean delete(@RequestParam("bucketName") String bucketName, @RequestParam("objectName") String objectName) {
        if (StringUtils.isBlank(bucketName) || StringUtils.isBlank(objectName)) {
            return false;
        }
        try {
            minIOUtil.delete(bucketName, objectName);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 获取文件外链
     *
     * @param bucketName bucket名称
     * @param objectName 文件名称
     * @param expires    过期时间 <=7 秒级
     * @return url
     */
    @RequestMapping("/getPresignedObjectUrl")
    public String getPresignedObjectUrl(@RequestParam("bucketName") String bucketName, @RequestParam("objectName") String objectName, @RequestParam("expires") Integer expires) {
        try {
            return minIOUtil.getPresignedObjectUrl(bucketName, objectName, expires);
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }


}
